package com.dm.cloud.auth.service.impl;

import cn.hutool.core.util.StrUtil;
import com.dm.cloud.auth.common.po.AuthClientDetails;
import com.dm.cloud.core.exception.CustomException;
import com.dm.cloud.tkmybatis.service.BaseMapper;
import com.dm.cloud.auth.mapper.AuthClientDetailsMapper;
import com.dm.cloud.auth.service.AuthClientDetailsService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

/**
   * @author hcx
   * @type AuthClientDetailsServiceImpl.java
   * @desc AuthClientDetails serviceImpl
   */
@Service

@Transactional

public class AuthClientDetailsServiceImpl implements AuthClientDetailsService {


    @Autowired
    private AuthClientDetailsMapper authClientDetailsMapper;

    public BaseMapper getMapper() {
        return authClientDetailsMapper;
    }

    @Override
    public AuthClientDetails getById(String clientId) {
        return authClientDetailsMapper.selectByPrimaryKey(clientId);
    }

    @Override
    public AuthClientDetails insert(AuthClientDetails authClientDetails) {
        if(StrUtil.isEmpty(authClientDetails.getClientId())){
            throw new CustomException("ClientId不能为空！");
        }
        if(StrUtil.isEmpty(authClientDetails.getClientSecret())){
            throw new CustomException("ClientSecret不能为空！");
        }
        if(authClientDetailsMapper.existsWithPrimaryKey(authClientDetails.getClientId())){
            throw new CustomException("ClientId重复！");
        }
        authClientDetailsMapper.insert(authClientDetails);
        return authClientDetails;
    }
}
